#![allow(dead_code)]

struct FromLrn {}

impl FromLrn {
    fn hello() {
        println!("From learnning");
    }
}

#[cfg(test)]
mod tests {
    // use super::*;
    use std::fs;
    use std::io;
    use std::num;

    #[test]
    /// 利用enum集合多种错误, 可以把错误类型统一, 同时可以保留底层错误信息
    fn impl_from_test() {
        #[derive(Debug)]
        enum CliError {
            IoError(io::Error),
            ParseError(num::ParseIntError),
        }

        impl From<io::Error> for CliError {
            fn from(error: io::Error) -> Self {
                CliError::IoError(error)
            }
        }

        impl From<num::ParseIntError> for CliError {
            fn from(error: num::ParseIntError) -> Self {
                CliError::ParseError(error)
            }
        }

        let res = open_and_parse_file("abc");
        println!("{:?}", res);

        fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
            let contents = fs::read_to_string(file_name)?;
            let num = contents.trim().parse::<i32>()?;
            Ok(num)
        }
    }
}
